package it.unica.informatica.enotes.capture;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;


/**
 * This class create a camera preview for the acquisition of images and videos created by the app
 *
 * @author Mirko Marras
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

   /*********************************************************************************************
    *                               CAMERA PREVIEW VARIABLES                                    *
    *********************************************************************************************/

   private SurfaceHolder mHolder;
   private Camera mCamera;

   /**
    * Construct a new camera preview to show
    *
    * @param context The activity that want to show the preview
    * @param camera The camera object from which eep the preview
    */
   public CameraPreview(Context context, Camera camera) {
      super(context);
      this.mCamera = camera;

      /* Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed */
      this.mHolder = getHolder();
      this.mHolder.addCallback(this);

      /* Deprecated setting, but required on Android versions prior to 3.0 */
      this.mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   }

   /**
    * Show the preview when the surface is created
    *
    * @param holder The surface holder where the preview is shown
    */
   public void surfaceCreated(SurfaceHolder holder) {
      /* The Surface has been created, now tell the camera where to draw the preview */
      try {
         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();
         mCamera.autoFocus(null);
      } catch (IOException e) {
         e.printStackTrace();
      }


   }

   /**
    * Destroy the preview when the surface is destroyed
    *
    * @param holder The surface holder where the preview is shown
    */
   public void surfaceDestroyed(SurfaceHolder holder) {
      /* Take care of releasing the Camera preview in your activity */
   }

   /**
    * Change the preview when the surface changed
    *
    * @param holder The surface holder where the preview is shown
    * @param format The format in which the preview is shown
    * @param width The width of the preview surface
    * @param height The height of the preview surface
    */
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      /* If your preview can change or rotate, take care of those events here.
         Make sure to stop the preview before resizing or reformatting it */

      /* Preview surface does not exist */
      if (mHolder.getSurface() == null) {
         return;
      }

      /* Stop preview before making changes */
      try {
         mCamera.stopPreview();
         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();
      } catch (Exception e) {
         e.printStackTrace();
      }

      /* Set preview size and make any resize, rotate or reformatting changes here */

      /* Start preview with new settings */
      try {
         mCamera.setPreviewDisplay(mHolder);
         mCamera.startPreview();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}